#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os

import wx
import wx.xrc

import ConfigModule as cm
import ProjectPanel as prj_pnl
import PathPanel as pth_pnl
import VCSPanelPro as vcs_pnl
import pysvn as svn
import SvnUtillity as su


###########################################################################
# Class MainFrame
###########################################################################
class MainFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(1280, 800), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)

        main_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # 主分割窗体
        self.main_splitter = main_splitter = \
            wx.SplitterWindow(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_LIVE_UPDATE)
        # 右侧上下分割窗体
        self.right_splitter = right_splitter = \
            wx.SplitterWindow(main_splitter, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SP_LIVE_UPDATE)

        # read configuration
        self.config_parser = config_parser = cm.ConfigParser()

        # 项目面板
        self.left_panel = left_panel = prj_pnl.PrjPanel(main_splitter, config_parser=config_parser)
        self.load_prj_list()

        # 路径面板
        self.path_panel = path_panel = pth_pnl.PathPanel(right_splitter)

        left_panel.Layout()
        right_splitter.Layout()
        # 工作面板
        # 文件列表面板
        self.file_list_panel = fl_pnl = vcs_pnl.VCSPanel(right_splitter)
        right_splitter.SplitHorizontally(path_panel, fl_pnl, 600)

        main_splitter.SplitVertically(left_panel, right_splitter, 320)
        main_sizer.Add(main_splitter, 1, wx.EXPAND, 5)

        self.SetSizer(main_sizer)
        self.Layout()

        self.Centre(wx.BOTH)
        # connection
        self.Bind(prj_pnl.EVT_CHANGE_PATH, self.on_prj_change, self.left_panel)
        self.Bind(prj_pnl.EVT_EDIT_PRJ_CONFIG, self.on_prj_edit, self.left_panel)
        self.Bind(pth_pnl.EVT_PATH_SELECTION_CHANGE, self.on_path_selection_changed, self.path_panel)

    def load_prj_list(self):
        config = self.config_parser.get_config()
        prj_list = [k for k in config]
        self.left_panel.set_prj_list(prj_list)

    def set_path(self, path):
        self.path_panel.set_path(path)

    def on_prj_change(self, evt):
        prj_name = evt.get_new_prj()
        config = self.config_parser.get_prj_config(prj_name)
        prj_path = config['local']
        self.set_path(prj_path)

    def on_prj_edit(self, evt):
        prj_name = evt.get_prj_name()
        config = self.config_parser.get_prj_config(prj_name)
        dialog = prj_pnl.ProjectEditDialog(self, self.config_parser)
        dialog.set_config(prj_name, config)
        result = dialog.ShowModal()
        if result == wx.ID_OK:
            prj_config = dialog.get_prj_config()
            self.config_parser.merge_config(prj_config)
            self.config_parser.persist_config()
        dialog.Destroy()

    def on_path_selection_changed(self, evt):
        fl_panel = self.file_list_panel
        fl_panel.clear_pages()
        client = svn.Client()
        paths = evt.get_paths()

        add_list = []
        del_list = []
        change_list = []
        cflct_list = []
        unv_list = []
        for path in paths:
            if not su.is_svn_path(path):
                return
            changes = client.status(path)
            add_list.extend([f.path for f in changes if f.text_status == svn.wc_status_kind.added])
            del_list.extend([f.path for f in changes if f.text_status == svn.wc_status_kind.deleted])
            change_list.extend([f.path for f in changes if f.text_status == svn.wc_status_kind.modified])
            cflct_list.extend([f.path for f in changes if f.text_status == svn.wc_status_kind.conflicted])
            unv_list.extend([f.path for f in changes if f.text_status == svn.wc_status_kind.unversioned])

        fl_panel.set_items(vcs_pnl.build_file_list(
            add_list=add_list,
            del_list=del_list,
            chg_list=change_list,
            cfl_list=cflct_list,
            unv_list=unv_list
        ))


if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None)
    # frame.set_path(prj_config['local'])
    # frame.set_prj_list(prj_list)

    app.SetTopWindow(frame)
    frame.Show()
    app.MainLoop()
